package com.eastseeway.radioproject.retrofit;

import static com.eastseeway.radioproject.retrofit.ApiService.HTTP_HOST;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static RetrofitClient retrofitClient;
    private static final int READ_TIME_OUT = 10;
    private static final int CONNECT_TIME_OUT = 10;
    private final Retrofit mRetrofit;

//    private RetrofitClient() {
//        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
//        LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        OkHttpClient build = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).addInterceptor(LoginInterceptor).readTimeout(60, TimeUnit.SECONDS).build();
////        okHttpClient = build;
////        retrofit = new Retrofit.Builder().client(build).baseUrl(REQUEST_PATH).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
//        mRetrofit = new Retrofit
//                .Builder()
//                .client(build)
//                .baseUrl(HTTP_HOST)
//                .addConverterFactory(GsonConverterFactory.create())
////                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .build();
//    }

    public RetrofitClient() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        //设置连接和读取时间
        builder.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS);
        builder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS);
        //添加日志拦截器
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addNetworkInterceptor(interceptor);
        //添加数据请求统一处理拦截器
        builder.addInterceptor(new Interceptor() {
            @NonNull
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                Request original = chain.request();
                Request request = original.newBuilder()
                        .method(original.method(), original.body())
                        .build();
                return chain.proceed(request);
            }
        });
        OkHttpClient client = builder.build();
        mRetrofit = new Retrofit.Builder()
                .baseUrl(ApiService.HTTP_HOST)
                //添加自定义的CustomGsonConverterFactory 统一处理返回数据
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    public static RetrofitClient getInstance() {
        if (retrofitClient == null) {
            retrofitClient = new RetrofitClient();
        }
        return retrofitClient;
    }

    public ApiService getServiceApi() {
        return mRetrofit.create(ApiService.class);
    }
}
